home *** CD-ROM | disk | FTP | other *** search
- Path: cea.fr!usenet
- From: Xavier Tarrago <tarrago@lcus15.saclay.cea.fr>
- Newsgroups: comp.lang.c++
- Subject: Re: How to print a enum variable's names
- Date: 8 Jan 1996 09:11:55 GMT
- Organization: CEA Commissariat a l'Energie Atomique, France.
- Message-ID: <4cqn4r$9mp@news.cea.fr>
- References: <4cjohq$k5u@lsi.lsil.com>
- NNTP-Posting-Host: lcus15.saclay.cea.fr
-
- song@lsil.com (Song Liang) wrote:
- >
- > Suppose you have a variable foo of type "enum bar_type {CAR TRUCK VAN}", if
- > you do "cout << foo", it will print either 0, 1 or 2. Is there a simple trick
- > to print the names, i.e CAR, TRUCK or VAN?
- >
-
- #include <iostream.h>
- enum CarType {
- VAN,
- TRUCK,
- CAR
- };
-
- ostream& operator<<( ostream& os, CarType ct)
- {
- switch( ct){
- case VAN :
- os << "VAN";
- break;
-
- case TRUCK :
- os << "TRUCK";
- break;
-
- case CAR :
- os << "CAR";
- break;
-
- default:
- os << "Unknown Car Type";
- break;
- }
- return os;
- }
-
- void main()
- {
- CarType ct;
- ct = VAN;
- cout << "Type " << ct << " Valeur " << (int)ct << '\n';
- return;
- }
-
- result (HP C++):
- Type VAN Valeur 0
-
- Hope it helps
- X.Tarrago
-